<html>
	<head>
		<title>Judo Invorio</title>
		<link href="lib/jqueryui/development-bundle/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css"/>		
		<link href="judoinvorio.css" rel="stylesheet" type="text/css"/>
	</head>
<body>
<?php
include_once("include/config.php");
include_once("include/auth.lib.php");

list($status, $user) = auth_get_status();

if ($status == AUTH_NOT_LOGGED){
	$uname = strtolower(trim($_POST['username']));
	$passw = strtolower(trim($_POST['password']));

	if($uname == "" or $passw == "") {
		$status = AUTH_INVALID_PARAMS;
	} else {
		list($status, $user) = auth_login($uname, $passw);
		if (!is_null($user)){
			list($status, $uid) = auth_register_session($user);
		}
	}
}

$url = 'atleti.php';
$auth = '';
switch($status){
	case AUTH_LOGGED:
		$auth = 'AUTH_LOGGED';
		header("Refresh: 0;URL=$url");
	break;
	case AUTH_INVALID_PARAMS:
		$auth = 'AUTH_INVALID_PARAMS';
		header("Refresh: 2;URL=index.php");
		echo '<br/><br/><div class="ui-state-error" align="center">Hai inserito dati non corretti ... attendi il reindirizzamento</div>';
	break;
	case AUTH_LOGEDD_IN:
		switch(auth_get_option("TRANSICTION METHOD")){
			case AUTH_USE_LINK:
				$auth = 'AUTH_USE_LINK';
				header("Refresh: 5;URL=$url?uid=".$uid);
			break;
			case AUTH_USE_COOKIE:
				$auth = 'AUTH_USE_COOKIE';
				header("Refresh: 0;URL=$url");
				setcookie('uid', $uid, time()+3600*365);
			break;
			case AUTH_USE_SESSION:
				$auth = 'AUTH_USE_SESSION';
				header("Refresh: 5;URL=$url");
				$_SESSION['uid'] = $uid;
			break;
		}
	break;
	case AUTH_FAILED:
		header("Refresh: 2;URL=index.php");
		echo '<div align="center">Fallimento durante il tentativo di connessione ... attendi il reindirizzamento</div>';
	break;
}
?>

</body>
</html>